package com.st.mall.user.service.impl;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.st.mall.user.mapper.IUserMapper;
import com.st.mall.user.model.UserModel;
import com.st.mall.user.service.IUserService;

@Service
public class UserServiceImpl implements IUserService {
	
	@Autowired
	private IUserMapper mapper;
	
	/**
	 * 	A:添加成功
	 *	B:添加失败
	 *	C:帐号重复
	 */
	public String insert(UserModel userModel) {
		String code = userModel.getCode();
		String name =  userModel.getName();
		if(mapper.selectId(code)!=null||name.equals("administrator")) {// 帐号已存在或为管理员帐号
		
			return "C";
		}
		int result = mapper.insert(userModel);
		if(result==1){
			return "A";
		}else {
			return "B";
		}
	}
	
	/**
	 *	A:注册成功
	 *	B:注册失败
	 *	C:帐号重复
	 *	F:验证码错误
	 */
	@Override
	public String reg(UserModel userModel, String authcode, Object authCodeSession) {
		String code = userModel.getCode();
		String name =  userModel.getName();
		if(mapper.selectId(code)!=null||name.equals("administrator")) {// 帐号已存在或为管理员帐号
			return "C";
		}
		if(!authcode.equalsIgnoreCase((String) authCodeSession)){
			return "F";
		}
		int result = mapper.insert(userModel);
		if(result==1){
			return "A";
		}
		else {
			return "B";
		}
		
	}

	@Override
	public List<UserModel> selectAll(UserModel userModel) {
		List<UserModel> list = mapper.selectAll(userModel);
		return list;
	}

	public UserModel selectId(UserModel userModel) {
		UserModel model = mapper.selectId(userModel.getCode());
		return model;
	}
	
	/**
	 *	A:帐号不存在 
	 *	B:密码不正确
	 *	C:登录成功
	 *	S:管理员账户
	 */
	public String login(UserModel userModel,String authcode,Object authCodeSession) {
		UserModel model = selectId(userModel);
		if (model == null) {
			return "A";
		} else if (!model.getPass().equals(userModel.getPass())) {
			return "B";
		} else if(!authcode.equalsIgnoreCase((String) authCodeSession)) {
			return "F";	//验证码错误
		}else if(model.getStatus()==1) {
			return "S";
		}else {
			return "C";
		}
	}

	@Override
	public int delete(String code) {
		int result = mapper.delete(code);
		return result;
	}

	@Override
	public List<UserModel> selectModel(UserModel userModel) {
		String code = userModel.getCode(); // 为什么没pass?
		if (code != null) {
			userModel.setCode("%" + code + "%");
		}
		String name = userModel.getName();
		if (name != null) {
			userModel.setName("%" + name + "%");
		}
		List<UserModel> list = mapper.selectModel(userModel);
		return list;
	}

	@Override
	public int selectCount(UserModel userModel) {
		int result = mapper.selectCount(userModel);
		return result;
	}

	@Override
	public int update(UserModel userModel) {
		int result = mapper.update(userModel);
		return result;
	}



}
